#include <math.h>

#include <vector>
#include <string.h>

#include "WA_DlgTarget.h"

const char *TargetWordsDialog::mascotName[12] =
{
	"yoshi", 
	"cloudbaby",
	"dogthing",
	"bird",
	"frog",
	"blarg",
	"fish",
	"penguin",
	"koopa",
	"plant",
	"wizard",
	"bowser"
};



TargetWordsDialog::TargetWordsDialog( QWidget *parent, int wordCount, int timeLimit ) : 
	QDialog( parent)	
{
	setupUi( this );
	connect( btnNanowrimo, SIGNAL( pressed() ), this, SLOT( presetNanowrimo() ) );
	connect( btnSuggestTime, SIGNAL( pressed() ), this, SLOT( suggestTimeLimit() ) );
	connect( btnSuggestWords, SIGNAL( pressed() ), this, SLOT( suggestWordCount() ) );

	currMascot = 0;

	connect( dialWordCount, SIGNAL( sliderMoved(int) ), this, SLOT( targetWordsDial(int) ) );
	connect( dialTimeLimit, SIGNAL( sliderMoved(int) ), this, SLOT( timeLimitDial(int) ) );

	spinWordCount->setValue( wordCount );
	spinTimeLimit->setValue( timeLimit );

	// now add the mascot buttons
	QGridLayout *gridMascots = new QGridLayout( grpMascot );
	for (int i=0; i < 12; i++)
	{
		MascotButton *btn = new MascotButton( grpMascot, i );
		btn->m_ownerDlg = this;
		m_mascotBtn.push_back( btn );

	
		QString iconName;
		iconName.sprintf( ":/mascots/mascots/%02d_%s_profile_sel.png", i, mascotName[i] );
		btn->m_selIcon = QIcon( iconName );
		

		iconName.sprintf( ":/mascots/mascots/%02d_%s_profile.png", i, mascotName[i] );
		btn->m_unselIcon = QIcon( iconName );

		if (i == currMascot)
		{
			btn->setIcon( btn->m_selIcon );
		}
		else
		{
			btn->setIcon( btn->m_unselIcon );
		}
		btn->setIconSize( QSize( 36, 36 ) );

		connect( btn, SIGNAL( pressed() ), btn, SLOT( activate() ) );

		gridMascots->addWidget( btn, i / 6, i %6 );
	}

	
}

int TargetWordsDialog::getWordCount()
{
	return spinWordCount->value();
}

int TargetWordsDialog::getTimeLimit()
{
	return spinTimeLimit->value();
}

void TargetWordsDialog::presetNanowrimo()
{
	// some suggeted nanowrimo values
	spinWordCount->setValue( 1667 );
	spinTimeLimit->setValue( 120 );
}


void MascotButton::activate()
{
	if (m_ownerDlg)
	{
		m_ownerDlg->mascot( m_btnIndex );
	}
}

void TargetWordsDialog::mascot( int mascotIndex)
{
	//printf( "Mascot %d\n", mascotIndex );
	
	// unselect the old current index
	MascotButton *btnOldSelect = m_mascotBtn[ currMascot ];
	btnOldSelect->setIcon( btnOldSelect->m_unselIcon );

	// make the new selection current
	currMascot = mascotIndex;

	// select the next index
	MascotButton *btnSelect = m_mascotBtn[ currMascot ];
	btnSelect->setIcon( btnSelect->m_selIcon );
}

void TargetWordsDialog::getCurrentMascot( int &index, char *name )
{
	index = currMascot;
	strcpy( name, mascotName[currMascot] );
}

void TargetWordsDialog::timeLimitDial( int dialval )
{	
	int timeLeft = (int)( ((float)dialval/100.0) * 120.0);

	spinTimeLimit->setValue( timeLeft );	
}
void TargetWordsDialog::targetWordsDial( int dialval )
{
	
	float v = ((float)dialval/25.0) - 3.0;	
	int wordCount = (int)( exp(v) * (10000.0 / 2.718281828));

	// now "round" to the nearest nice value
	if (wordCount < 1000)
	{
		// nearest 10
		int n = wordCount / 10;
		wordCount = n*10;
	}
	else if (wordCount > 3000)
	{
		// nearest 100
		int n = wordCount / 100;
		wordCount = n*100;
	}
	else
	{
		// nearest 50
		int n = wordCount / 50;
		wordCount = n*50;
	}

	spinWordCount->setValue( wordCount );
}

void TargetWordsDialog::suggestTimeLimit()
{
	int wordCount = spinWordCount->value();
	int timeLimit = (int)(((float)wordCount / 1000.0) * 60.0);
	int n = timeLimit / 5;
	timeLimit = n * 5;

	spinTimeLimit->setValue( timeLimit );
}

void TargetWordsDialog::suggestWordCount()
{
	int timeLimit = spinTimeLimit->value();
	int wordCount = (int)(((float)timeLimit / 60.0) * 1000.0);
	int n = wordCount / 50;
	wordCount = n * 50;

	spinWordCount->setValue( wordCount );
}